package cn.kooki.app.duobao.ui.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ShapeDrawable;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.bus.BonusEvent;
import cn.kooki.app.duobao.data.bus.ChangePageEvent;
import cn.kooki.app.duobao.data.bus.WinEvent;
import cn.kooki.app.duobao.ui.Activity.Comm.RootAcitivity;
import cn.kooki.app.duobao.ui.Activity.Goods.SearchResultActivity;
import cn.kooki.app.duobao.ui.Activity.User.DuobaoBonusAcitivity;
import cn.kooki.app.duobao.ui.Activity.User.WinRecordActivity;
import cn.kooki.app.duobao.util.ScreenUtils;
import cn.kooki.app.duobao.util.manager.AppManager;
import de.greenrobot.event.EventBus;

/**
 * Created by xiong on 2016/1/8.
 */
public class PushWinDialogUtils {

    public static void showDilaog(final Context context, WinEvent object) {

        final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        View dialogview = LayoutInflater.from(context).inflate(R.layout.layout_prize_dialog, null);
        TextView prize_period = (TextView) dialogview.findViewById(R.id.prize_period);
        TextView prize_gname = (TextView) dialogview.findViewById(R.id.prize_gname);
        final Button gowin = (Button) dialogview.findViewById(R.id.btn_go_win);
        gowin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                goTowin(context);
            }
        });
        View close = dialogview.findViewById(R.id.dialog_close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        prize_gname.setText(object.goodname);
        prize_period.setText(object.uname);

        alertDialog.show();
        alertDialog.getWindow().setContentView(dialogview);
        alertDialog.getWindow().setBackgroundDrawableResource(R.drawable.tranport_bg);
        alertDialog.getWindow().setLayout(ScreenUtils.dpToPxInt(context, 300), ScreenUtils.dpToPxInt(context, 360));
        alertDialog.getWindow().setGravity(Gravity.CENTER);
    }

    private static void goTowin(Context context) {
        if (AppManager.hasActivity(WinRecordActivity.class)) {
            AppManager.getAppManager().getTopWith(WinRecordActivity.class);
        } else {
            Intent intent = new Intent(context, WinRecordActivity.class);
            context.startActivity(intent);
        }
    }


    public static void showBonusDiaog(final Context context, BonusEvent event) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        View dialogview = LayoutInflater.from(context).inflate(R.layout.layout_bonus_dialog, null);
        TextView tvBonusTitle = (TextView) dialogview.findViewById(R.id.tv_bonus_title);
        Button bonusCheck = (Button) dialogview.findViewById(R.id.bonus_check);
        View close = dialogview.findViewById(R.id.dialog_close);
        tvBonusTitle.setText(event.title);
        bonusCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                goBonus(context);
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog.show();
        alertDialog.getWindow().setContentView(dialogview);
        alertDialog.getWindow().setBackgroundDrawableResource(R.drawable.tranport_bg);
        alertDialog.getWindow().setLayout(ScreenUtils.dpToPxInt(context, 300), ScreenUtils.dpToPxInt(context, 330));
        alertDialog.getWindow().setGravity(Gravity.CENTER);
    }

    private static void goBonus(Context context) {
        if (AppManager.hasActivity(DuobaoBonusAcitivity.class)) {
            AppManager.getAppManager().getTopWith(DuobaoBonusAcitivity.class);
        } else {
            Intent intent = new Intent(context, DuobaoBonusAcitivity.class);
            context.startActivity(intent);
        }
    }


    public static void showFristDilaog(final Context context, final boolean isbuy) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        View dialogview = null;
        if (isbuy) {
            dialogview = LayoutInflater.from(context).inflate(R.layout.layout_first_buy_dialog, null);
        } else {
            dialogview = LayoutInflater.from(context).inflate(R.layout.layout_first_dialog, null);
        }

        final Button gowin = (Button) dialogview.findViewById(R.id.btn_go_use);
        View close = dialogview.findViewById(R.id.btn_close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        gowin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goAction(context, isbuy);
                alertDialog.dismiss();
            }
        });

        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.show();
        alertDialog.getWindow().setContentView(dialogview);
        alertDialog.getWindow().setBackgroundDrawableResource(R.drawable.tranport_bg);
        alertDialog.getWindow().setLayout(ScreenUtils.dpToPxInt(context, 340), ScreenUtils.dpToPxInt(context, 340));
        alertDialog.getWindow().setGravity(Gravity.CENTER);
    }

    private static void goAction(Context context, boolean isbuy) {
        if (isbuy) {
            Intent intent = new Intent(context, DuobaoBonusAcitivity.class);
            context.startActivity(intent);
        } else {
            EventBus.getDefault().post(new ChangePageEvent());
            AppManager.getAppManager().getTopWith(RootAcitivity.class);
            Intent intent = new Intent(context, SearchResultActivity.class);
            intent.putExtra("title","全部商品");
            context.startActivity(intent);

        }
    }

}
